{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "dfc33e27-192a-4676-9448-0b39bf907b1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c92adf2e-a913-4232-aec1-bd5fbb0edc82",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = \"SimHei\"\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e142ca89-0180-4736-9f18-39384f531cad",
   "metadata": {},
   "source": [
    "# 手写线性回归梯度下降算法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d245dd1-6397-44b8-afba-3d8136606a2d",
   "metadata": {},
   "source": [
    "手动实现算法的三个步骤：\n",
    "1. 找到算法的预测函数，有两种形式 y = wx + b, y = θx\n",
    "2. 找到算法的目标函数即损失函数，最小二乘法\n",
    "3. 使用梯度下降找到损失最小的时候的系数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a3a1054-8b88-466e-96a8-440b3a4ba41d",
   "metadata": {},
   "source": [
    "## 一、构造数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f6ecdac-96d4-48b8-bc2f-33a970c11f6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAD4CAYAAADFAawfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAOoElEQVR4nO3dXYxc9X3G8efp2iiLi7JJPUJhDV0uIkcIRIxGiIQKtTgpoCCwuKiIRBShRHuTFxJVRKFShXpFJaIqXFSVViRpqiCi1nXcKG15UUIVcREnY68TA8ZqQnjxAPGgakMTbctLf73Y2WBWuzNnOec/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+6hUoqIvqSvSHpe0kuSfh0Rj9Q71cScHxEvDW+/LOn8Kl40l6BurXEXDW4S2zdKOhMRR+ueZYJ2SLpC0t9FxD5Jv1VF/xzO1XAve7PW/id1gaRdtm+rd6rJi7Vznys5/zmXoO5LuvCsr/cM72u01BcNztDVkm6y/azW1lvX2v5WvSMld1rS6YhY/9fSQa0Fd5N9RNIvI2IQEa9LOiTpwzXPNCm/sv0+SRr+fqaKF80lqH8i6f22L7Z9jtYOPHy35pmSmsRFg3MTEXdFxJ6IWNDaf+MfRESjP2lFxMuSXrC9d3jXfklP1TjSJDwv6Srb5w7/nu9Xww+gnuW7kj45vP1JSf9SxYsWubhtchHxhu3PSnpYa0eIvx4RT9Y8VmoTuWgwsvA5SQ8MP4Q8I+n2mudJKiKO2D4o6ZjWzm5aVgPr5LYflPTHknbbPi3pbkl/LekfbX9Kaz/q+c8q+V5UyAEgb7msPgAAWyCoASBzBDUAZI6gBoDMEdQAkDmCGgAyR1ADQOb+H6The3SxDS+xAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.linspace(0, 10, num=20)\n",
    "y = np.linspace(2, 8, num=20)\n",
    "plt.scatter(x, y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c032c6f-f203-4437-a045-3dd746a3107b",
   "metadata": {},
   "source": [
    "给数据加点噪声"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bcadd15a-c86b-4c6e-868d-173dc787d8d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAD4CAYAAADFAawfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAm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/f+hdkvSC33/wfm7WdIrZRdRAl53tfC60/RHEdF1WncuQT2obDciYqbsOorG664WXvfgofUBAIkjqAEgcQT1evNlF1ASXne18LoHDD1qAEgcV9QAkDiCGgASV/mgtv0O28/YPmv7OdsPl11TkWyP2F6y/UTZtRTF9pjto7aft33O9vvKrqkItj/b+Td+xvYR2zeVXVMebH/D9kXbZ6469ge2n7b9086fbyuzxl5VPqglvS7pcxHxbkl3Svqk7XeXXFORHpZ0ruwiCvZVSU9GxLskvUcVeP22JyR9WtJMRNwmaUTSg+VWlZtvSfrghmNfkPS9iHinpO91vh8YlQ/qiHgpIk52Hv9G7TdtJRbGtr1b0oclPVp2LUWx/VZJd0v6uiRFxGsRsVJqUcXZIalme4eknZIulFxPLiLih5L+e8PhByR9u/P425Jmi6xpuyof1FezPSlpWtLxkkspyiOSPi/pdyXXUaRbJbUkfbPT8nnU9q6yi8pbRDQlfVnSi5JekvS/EfFUuVUV6paIeKnz+GVJt5RZTK8I6g7bb5b0XUmfiYhfl11P3mx/RNLFiDhRdi0F2yHpDklfi4hpSa9qwG6Db0SnJ/uA2v+jGpe0y/ZD5VZVjmiPSR6occkEtSTbo2qH9GMRsVB2PQW5S9L9tn8h6R8l3WP7O+WWVIjzks5HxNpd01G1g3vYfUDSzyOiFRGXJC1Ien/JNRXpV7bfLkmdPy+WXE9PKh/Utq12v/JcRHyl7HqKEhEHI2J3REyq/aHS9yNi6K+wIuJlSb+0vbbF/V5JZ0ssqSgvSrrT9s7Ov/m9qsCHqFf5V0kf7zz+uKR/KbGWnlU+qNW+svyY2leUpzpf95VdFHL1KUmP2X5W0u2S/rbccvLXuYM4KumkpNNqv/cHdkr19dg+IunHkqZsn7f9CUlfknSv7Z+qfXfxpTJr7BVTyAEgcVxRA0DiCGoASBxBDQCJI6gBIHEENQAkjqAGgMQR1ACQuP8HetBdvRXO6awAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.linspace(0, 10, num=20) + np.random.randn(20)\n",
    "y = np.linspace(2, 8, num=20) + np.random.randn(20)\n",
    "plt.scatter(x, y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dae7bbf8-07f4-4c69-bc3a-89387dc25880",
   "metadata": {},
   "source": [
    "构造了20个样本，1个特征的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d703fa5b-0982-4557-8a2a-228e4bd725c9",
   "metadata": {},
   "source": [
    "用正规方程计算一下参数 $\\theta = (X^TX)^{-1}X^Ty$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "28fabb44-7c8a-4438-a79e-db5bf7c521a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.57099316, 0.74573265])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用正规方程计算一下参数\n",
    "theta1 = np.linalg.inv(X.T @ X) @ X.T @ y\n",
    "theta1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4ab44d3-e2c5-49b0-b286-b2a16a07a9b8",
   "metadata": {},
   "source": [
    "## 二、手写梯度下降算法来拟合线性回归模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b69afcd3-0cd9-46ed-8391-877b1741f9df",
   "metadata": {},
   "source": [
    "### （1）y = θX"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09ffbc3a-7656-44fe-b326-676caa014664",
   "metadata": {},
   "source": [
    "【注】如果要预测的模型是$y = \\theta X$必须要给X前面加上一列$X_0=1$，以便方程可以得到偏置$b=w_0X_0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4bd7c865-3614-4a73-ae84-8f64e8d645e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(shape=(20, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8479c908-91d8-4358-8a93-8db67298d195",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.77362166],\n",
       "       [ 0.99416518],\n",
       "       [ 2.02715264],\n",
       "       [ 1.22334063],\n",
       "       [ 2.69921157],\n",
       "       [ 4.24102345],\n",
       "       [ 3.47108442],\n",
       "       [ 4.46962467],\n",
       "       [ 3.82907402],\n",
       "       [ 4.18540762],\n",
       "       [ 5.41570647],\n",
       "       [ 4.50525504],\n",
       "       [ 5.8434574 ],\n",
       "       [ 6.56405698],\n",
       "       [ 6.86966237],\n",
       "       [ 7.59159917],\n",
       "       [ 7.39748131],\n",
       "       [ 7.55962123],\n",
       "       [ 8.74535097],\n",
       "       [10.82526155]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.reshape(-1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "00679af5-17e1-48de-81b9-1082fcd719db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  1.77362166],\n",
       "       [ 1.        ,  0.99416518],\n",
       "       [ 1.        ,  2.02715264],\n",
       "       [ 1.        ,  1.22334063],\n",
       "       [ 1.        ,  2.69921157],\n",
       "       [ 1.        ,  4.24102345],\n",
       "       [ 1.        ,  3.47108442],\n",
       "       [ 1.        ,  4.46962467],\n",
       "       [ 1.        ,  3.82907402],\n",
       "       [ 1.        ,  4.18540762],\n",
       "       [ 1.        ,  5.41570647],\n",
       "       [ 1.        ,  4.50525504],\n",
       "       [ 1.        ,  5.8434574 ],\n",
       "       [ 1.        ,  6.56405698],\n",
       "       [ 1.        ,  6.86966237],\n",
       "       [ 1.        ,  7.59159917],\n",
       "       [ 1.        ,  7.39748131],\n",
       "       [ 1.        ,  7.55962123],\n",
       "       [ 1.        ,  8.74535097],\n",
       "       [ 1.        , 10.82526155]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.hstack((np.ones(shape=(20, 1)), x.reshape(-1, 1)))\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d45643fb-8eca-41b0-904c-7afc434415e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(20, 2)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 20个样本；2个特征，其中一个是w，另一个是b\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2e4969a5-3a57-45d1-a362-5b6cd9aeb831",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义预测函数\n",
    "def linear(theta):\n",
    "    return np.dot(X, theta)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fca95a53-3b6e-4a06-aa6f-a51aacb4ae23",
   "metadata": {},
   "source": [
    "预测函数 $y = X\\theta$ 这里为什么是X左乘θ？原因是我们求的是参数θ，θ才是未知数，于是X变成了系数，放在了左边"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "89f49b63-7ed6-4cb5-a62c-7947cf6d2e90",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义损失函数\n",
    "def loss(y_true, y_pred):\n",
    "    return np.sum((y_true - y_pred)**2 / (20 * 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "197dec9b-e9b6-4d39-9760-ab9c425d8eda",
   "metadata": {},
   "source": [
    "损失函数 $L = \\frac{1}{2 \\times 20} \\sum_{i=1}^{20} (y - \\hat y)^2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "33d2ff33-edae-496e-9f6d-09008625acf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.00354378, -0.00959828])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化θ\n",
    "# 一般会把θ初始化为一个比较小数，不能是0\n",
    "theta = np.random.randn(2) * 0.01\n",
    "theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "af2d63c5-42e5-464f-8282-cdc50e598b04",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化学习率\n",
    "learning_rate = 0.01\n",
    "# 定义最大迭代次数\n",
    "max_iter = 2000\n",
    "# 迭代计数器\n",
    "epoch = 0\n",
    "\n",
    "# 梯度下降算法\n",
    "while True:\n",
    "    # 计算预测值\n",
    "    y_pred = linear(theta)\n",
    "    # 计算参数的梯度\n",
    "    d_theta = -np.dot((y - y_pred), X) / 20\n",
    "    # 更新参数\n",
    "    theta = theta - learning_rate * d_theta\n",
    "    # 更新迭代计数器\n",
    "    epoch += 1\n",
    "    # 退出条件\n",
    "    if epoch > max_iter:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "86dda219-4dc5-4590-8428-fb71cb186104",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.54790549, 0.74939231])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dc0f003d-63a1-482b-a7cf-ad33735fcbdb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正规方程计算结果 [1.57099316 0.74573265]\n",
      "梯度下降计算结果 [1.54790549 0.74939231]\n"
     ]
    }
   ],
   "source": [
    "# 对比一下梯度下降算法算出的参数和正规方程算出的参数\n",
    "print(f'正规方程计算结果 {theta1}')\n",
    "print(f'梯度下降计算结果 {theta}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "599907fc-b5af-4636-a551-5c51b681c3d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD4CAYAAADM6gxlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAoj0lEQVR4nO3dZ3yUVd7G8d8hhYRACE0gtNAMKAhILPQgVQRF3V0FdUVUEOv6aBRQFHARFNiiK3b54FpYdLORpoBCqAIGQemg1ASkBEJCSEg7zwtCJBACKTP3THJ93zi5p9z/GcmVM+c+xVhrERER71HB6QJERKRoFNwiIl5GwS0i4mUU3CIiXkbBLSLiZRTcIiJeRsEtIuJlfC/nQcaY2sCX1touxhg/4H9AdeADa+1HhT23Zs2aNiwsrMSFioiUJ+vWrTtqra1V0H2XDG5jTDVgBhCUe+gJIM5aO9YYE22M+cJam3Kx54eFhREXF1ecukVEyi1jzN6L3Xc5XSXZwF1Acu7PkcCs3NurgIiSFCciIkVzyeC21iZba0+ccygISMi9nQzUPv85xphhxpg4Y0zckSNHSqdSEREBindx8iQQmHu7ckGvYa19z1obYa2NqFWrwC4aEREppsu6OHmedUBn4EugDbC6qC+QmZlJfHw86enpxTh9+RIQEED9+vXx8/NzuhQR8RDFCe4ZwHxjTBfgKmBNUV8gPj6eKlWqEBYWhjGmGCWUD9ZaEhMTiY+Pp3Hjxk6XIyIe4rK7Sqy1kbn/3Qv0AlYCPa212UU9aXp6OjVq1FBoX4Ixhho1auibiYjkU5wWN9baA/w+sqRYFNqXR5+TiJxPMycvw7/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XC1slyxcUAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画图对比一下\n",
    "plt.scatter(x, y)\n",
    "plt.plot(x, theta1[1] * x + theta1[0], c='r', label='正规方程')\n",
    "plt.plot(x, theta[1] * x + theta[0], c='c', label='梯度下降')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e11c12c-d5ab-4103-89f7-16f1597af43c",
   "metadata": {},
   "source": [
    "### （2）y = wx + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "35d26277-98db-4332-984b-de9aea72c5e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.01516416])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0.])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 初始化w和b\n",
    "w = np.random.randn(1) * 0.01\n",
    "b = np.zeros(shape=1)\n",
    "display(w, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f0078432-50b0-4f61-b9dd-22713cc6538a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义模型 y = wx +b\n",
    "def linear(w, b):\n",
    "    return w * x + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2e2cc600-2971-43f2-bb3c-9c94a21b7dd8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w:[0.92828102], b:[0.41935088], loss:0.7025352108077634\n",
      "w:[0.89432287], b:[0.63358254], loss:0.6551748757910346\n",
      "w:[0.86668171], b:[0.80796224], loss:0.6237958608684216\n",
      "w:[0.84418244], b:[0.94990334], loss:0.603005412972186\n",
      "w:[0.82586854], b:[1.06544018], loss:0.589230515168922\n",
      "w:[0.81096144], b:[1.15948453], loss:0.5801038330292494\n",
      "w:[0.7988274], b:[1.23603449], loss:0.5740568678852408\n",
      "w:[0.78895057], b:[1.29834443], loss:0.5700503968705541\n",
      "w:[0.78091105], b:[1.34906329], loss:0.5673958735503017\n",
      "w:[0.77436707], b:[1.3903473], loss:0.565637095305824\n",
      "w:[0.76904042], b:[1.42395154], loss:0.5644718009986877\n",
      "w:[0.76470464], b:[1.45130462], loss:0.5636997247994815\n",
      "w:[0.76117542], b:[1.47356941], loss:0.5631881787859718\n",
      "w:[0.75830271], b:[1.49169245], loss:0.5628492493846943\n",
      "w:[0.7559644], b:[1.50644418], loss:0.5626246886687144\n",
      "w:[0.75406106], b:[1.51845176], loss:0.562475903951392\n",
      "w:[0.75251178], b:[1.52822566], loss:0.5623773253019066\n",
      "w:[0.75125071], b:[1.53618139], loss:0.5623120111331583\n",
      "w:[0.75022423], b:[1.54265717], loss:0.5622687366446\n",
      "w:[0.74938869], b:[1.54792831], loss:0.5622400647439103\n"
     ]
    }
   ],
   "source": [
    "# 初始化学习率\n",
    "learning_rate = 0.01\n",
    "# 设置最大迭代次数\n",
    "max_iter = 2000\n",
    "# 迭代计数器\n",
    "epoch = 0\n",
    "# 损失列表\n",
    "loss_list = []\n",
    "\n",
    "# 手写梯度下降算法\n",
    "while True:\n",
    "    # 计算预测值\n",
    "    y_pred = linear(w, b)\n",
    "    # 计算参数的梯度\n",
    "    dw = -np.dot((y - y_pred), x) / 20\n",
    "    db = -(y - y_pred).mean()\n",
    "    # 更新w和b\n",
    "    w = w - learning_rate * dw\n",
    "    b = b - learning_rate * db\n",
    "    # 更新迭代计数器\n",
    "    epoch += 1\n",
    "    # 打印输出结果\n",
    "    if epoch % 100 == 0:\n",
    "        loss_ = loss(y, y_pred)\n",
    "        print(f'w:{w}, b:{b}, loss:{loss_}')\n",
    "        loss_list.append(loss_)\n",
    "    # 退出条件\n",
    "    if epoch > max_iter:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4ecfa7fc-d821-4a5a-bf91-64d99526c4ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正规方程：[1.57099316 0.74573265]\n",
      "梯度下降y=θX：[1.54790549 0.74939231]\n",
      "梯度下降y=wx+b：[[1.54797573 0.74938117]]\n"
     ]
    }
   ],
   "source": [
    "# 对比三个方法求出来的参数\n",
    "print(f'正规方程：{theta1}')\n",
    "print(f'梯度下降y=θX：{theta}')\n",
    "print(f'梯度下降y=wx+b：{np.array([b,w]).reshape(1, -1)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e2cc984d-18ba-46fa-a7df-5453ce91acff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAECCAYAAAD+VKAWAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAjsUlEQVR4nO3de3hV9Z3v8fd374SEEEIIhEuCQrgoXhCQQBFBwY61OlUKUx3n6ei0Pa2nl7Gd02nn1NYzp+047ZR27Jyx6hTLdKbVOsN0vF9qrUhFrUoQFEVU1ICGW4RAgISQZH/PH3sFQtwbAtkrayf783qe/ey11/qtvb4uEz5Zt9/P3B0REZFY1AWIiEh2UCCIiAigQBARkYACQUREAAWCiIgE8qIu4GQNHz7cx40bF3UZIiJ9ypo1a9539/JUy/psIIwbN46ampqoyxAR6VPMbHO6ZTplJCIigAJBREQCCgQREQEUCCIiElAgiIgIoEAQEZGAAkFERIAcDIS6Pc388LGN1O1pjroUEZGsknOBcKCljVuffItnN70fdSkiIlkl5wJhYnkxQ4vyeeGd3VGXIiKSVUIJBDNbZmbPmtmNaZZ/wcxWBq91ZvbT7qyXCbGYUT2ujNW1CgQRkc4yHghmthiIu/scoMLMJnV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2/hwI3RmopzttQhFcRF4O3ODu6TrpSzWQUG/pzrYj23+dzAOeP8byKPdhh+7up0j2p5kNBe4GPpPmL98Oj5nZaDMrAi4BXumN+k5gu1H+PC4EHjlOm6j2X/dFfREjrBdQQvKX/2bgNZI/IDcdp82QXqzvC0ADsDJ4/d8U9Z0NvAysB/6+l/ffUdsGyoCfdWkTIznU6f8DXgeqIvj//D1gcTB9Zpbtw5XB+1jg1WA/rQbiwEXAX3Zp/4F2vVTfD4BtnX4WL0xT3wKSf9m+3HVZyPV9YLtp/l/36u9zR33B9K+Aczt9jnz/ncyrXz+pHPzlczHwlCcH6jmpNpKemQ0E/pjkHRdvR11PtrLkCIJzgcf8GH+Fd7edpKbf557p14EgIiLd15+vIYiIyAlQIIiICKBAEBGRgAJBpJeZ2afM7FNR1yHSlQJBREQA6M2+cUT6rOBhol8AI0g+01APfAgoCqavdvc2M7sFmEaym4xrg/efBPNagauDr5xqZiuAUcBV7p59DylJztERgkj3XAe84u4XAKOBc4BV7n4hsANYaGYfAwrdfR7wa+B/A5cDee5+PvAjYEbwfTNJPq36beCK3vwPEUlHgSDSPaeT7DV1JTAeqCTZVQIknzwdR/Lp2Y5uNJ4HzgAmAy8AuPtDJDs4A7jb3VtJ9nL7gbEwRKKgQBDpnteBf3L3+cCNwBZgVrBsOrCJZLcTs4N5s4PPG0keDWBmnwT+Llh+oFeqFjkBuoYg0j13AD83s0+T7Fr5DWBmcMSwHXjQ3RNm9tFgJK09HLmGcKmZPQU0AdeQ7OpDJOuo6wqRk2Bm3ybZudnKiEsRyRgFgoiIALqGICIiAQWCiIgACgQREQkoEEREBFAgiIhI4P8D8iRoN7ShmbUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 损失图像\n",
    "plt.plot(loss_list)\n",
    "plt.xlabel('epoch')\n",
    "plt.ylabel('loss')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "8090fe85-c6f0-4663-8dc3-bc6a5242eb2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD4CAYAAADM6gxlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAs0UlEQVR4nO3deVxU1f/H8dcBEXAFFTM01xQ1cUG0cknS/GqpydddUzMNN7TU0gTLhTRIsUzTtNKyn7tGJi65lFRmZeCauOQ3M8UdxRVkmfP7Q5hCAdnvDPN5Ph49mjlz586HQd5z59xzzlVaa4QQQlgPO6MLEEIIkTMS3EIIYWUkuIUQwspIcAshhJWR4BZCCCsjwS2EEFZGglsIIaxMsexspJR6CFintW6tlHIAvgLKAZ9qrZdk9dwKFSro6tWr57lQIYSwJVFRUZe11m4ZPfbA4FZKuQJLgZKpTaOBSK31VKVUmFJqrdb6RmbPr169OpGRkbmpWwghbJZS6lRmj2WnqyQF6A1cT73vA6xJvb0b8M5LcUIIIXLmgcGttb6utb72r6aSQEzq7evAQ/c+Ryk1VCkVqZSKvHTpUv5UKoQQAsjdycmbgHPq7VIZ7UNr/bHW2ltr7e3mlmEXjRBCiFzK1snJe0QBrYB1QCPgl5zuICkpiTNnzpCQkJCLlxeFwcnJiSpVquDg4GB0KUKIe+QmuJcCm5VSrYH6wK853cGZM2coXbo01atXRymVixJEQdJaExsby5kzZ6hRo4bR5Qgh7pHtrhKttU/q/08B7YGfgGe01ik5fdGEhATKly8voW2hlFKUL19evhEJYaFyc8SN1vos/4wsyRUJbcsmvx8hLJfMnMyGDz/8kNjY2Gxv/6CLU1y4cIHvvvsur2UJISzU37GxtBg0iIgDBwpk/zYd3MnJydl6bOfOnTg5ObF7925q1aqFj48PPj4+1K5dO8Pn/uc//+H8+fP3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xvrXFcnN0oAnkope+BxwNbGBd/kn3M5pbCyLLSqYgvJEKApMEkpFaGU6m10QYVBa/2U1tpHa+0D7Ndav2x0TYVkMfC0UuoHYCR3T87agmDuTkK5BpQDVhpbTqGLAlql3m4E/GVcKTknE3CEEDZDKRWhtfZRSlUDNgM7gBbAE1rrFGOryz4JbiGETVJKuXP3qHvrPf3fFk+CWwghrIz0cQshhJWR4BZCCCsjwS2EEFZGglsIIayMBLcQQliZ/wc7ziPmlkutyAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画图对比一下\n",
    "plt.scatter(x, y)\n",
    "plt.plot(x, theta1[1] * x + theta1[0], c='r', label='正规方程')\n",
    "plt.plot(x, theta[1] * x + theta[0], c='c', label='梯度下降y=θX')\n",
    "plt.plot(x, w * x + b, c='k', label='梯度下降y=wx+b')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2607427-2b7e-440a-8bc4-e93d4e1e08b3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
